namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Web;
    using System.Web.Mvc;
    using Moq;
    using SimpleWebServices;
    using SimpleWebServices.Configuration;
    using SimpleWebServices.Data;
    using SimpleWebServices.Web;
    using SimpleWebServices.Web.Routing;
    using Xunit;

    public sealed class ExplicitUriConverterFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<ExplicitUriConverter<TestRecord>>()
                .DerivesFrom<UriConverter>()
                .IsConcreteClass()
                .IsSealed()
                .NoDefaultConstructor()
                .Implements<IConvertUri>()
                .Result);
        }

        [Fact]
        public void ctor_Type_RouteName()
        {
            Assert.NotNull(new ExplicitUriConverter<TestRecord>(typeof(Controller), new RouteName("name")));
        }

        [Fact]
        public void ctor_TypeNull_RouteName()
        {
            Assert.Throws<ArgumentNullException>(() => new ExplicitUriConverter<TestRecord>(null as Type, new RouteName("name")));
        }

        [Fact]
        public void ctor_Type_RouteNameNull()
        {
            Assert.Throws<ArgumentNullException>(() => new ExplicitUriConverter<TestRecord>(typeof(Controller), null as RouteName));
        }

        [Fact]
        public void prop_Urn_get()
        {
            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/test.en.html").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                AbsoluteUri expected = "urn://example.com/name/test";
                AbsoluteUri actual = new ExplicitUriConverter<TestRecord>(typeof(ResourceControllerOfIResourceDummy), "name").Urn;

                Assert.Equal<AbsoluteUri>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void prop_Urn_get_whenLanneg()
        {
            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/test.en").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                AbsoluteUri expected = "urn://example.com/name/test";
                AbsoluteUri actual = new ExplicitUriConverter<TestRecord>(typeof(ResourceControllerOfIResourceDummy), "name").Urn;

                Assert.Equal<AbsoluteUri>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void prop_Urn_get_whenConneg()
        {
            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/test").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                AbsoluteUri expected = "urn://example.com/name/test";
                AbsoluteUri actual = new ExplicitUriConverter<TestRecord>(typeof(ResourceControllerOfIResourceDummy), "name").Urn;

                Assert.Equal<AbsoluteUri>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                HttpContextFactory.Reset();
            }
        }
    }
}